using NExt.Core.Model.Enum;
using NExt.data;
using NExt.form;
using NExt.UI.Base.Js;
using NExt.UI.Edit.Base;

namespace NExt.Demo.Application.Entity.Bind
{
    public class Edit : EditPresenterBase
    {
        public TextField txtName;
        public TextField txtLastName;
        public DateField dtBirthday;
        public MoneyField mfSalary;
        public NumberField nfYearsOfExprience;
        public ComboBox cmbPhoneNumer;
        public Store phoneNumerStore;
        public Checkbox cBoxMember;
        public CheckboxGroup cBoxGroupHobbies;
        public Checkbox cBoxMusic;
        public Checkbox cBoxCinema;
        public Checkbox cBoxSports;
        public RadioGroup rgGender;
        public Radio radioMan;
        public Radio radioWoman;
        public TextArea txtNotes;
        public TriggerField tfReference;
        public Panel pnlInner;

        protected override void FormBuildComponents()
        {
            base.FormBuildComponents();
            txtName = new TextField();
            txtLastName = new TextField();
            dtBirthday = new DateField();
            nfYearsOfExprience = new NumberField();
            mfSalary = new MoneyField();
            cmbPhoneNumer = new ComboBox();
            phoneNumerStore = new Store();
            cBoxMember = new Checkbox();
            cBoxGroupHobbies = new CheckboxGroup();
            cBoxMusic = new Checkbox();
            cBoxCinema = new Checkbox();
            cBoxSports = new Checkbox();
            rgGender = new RadioGroup();
            radioMan = new Radio();
            radioWoman = new Radio();
            txtNotes = new TextArea();
            tfReference = new TriggerField();
            pnlInner = new Panel();
        }

        protected override void FormInitializeComponents()
        {
            base.FormInitializeComponents();
            txtName.dataMember = "Name";
            txtName.fieldLabel = "Name";
            txtLastName.dataMember = "LastName";
            txtLastName.fieldLabel = "LastName";
            dtBirthday.fieldLabel = "Birthday";
            dtBirthday.dataMember = "Birthday";
            nfYearsOfExprience.fieldLabel = "Exprience";
            nfYearsOfExprience.dataMember = "YearsOfExprience";
            mfSalary.fieldLabel = "Salary";
            mfSalary.dataMember = "Salary";

            BuildPhoneNumberCombo();

            cBoxMember.fieldLabel = "Member";
            cBoxMember.dataMember = "Member";

            cBoxMusic.boxLabel = "Music";
            cBoxMusic.dataMember = "Music";
            cBoxCinema.boxLabel = "Cinema";
            cBoxCinema.dataMember = "Cinema";
            cBoxSports.boxLabel = "Sports";
            cBoxSports.dataMember = "Sports";
            cBoxGroupHobbies.dataMember = "Hobbies";
            cBoxGroupHobbies.fieldLabel = "Hobbies";
            cBoxGroupHobbies.items.Add(cBoxMusic);
            cBoxGroupHobbies.items.Add(cBoxCinema);
            cBoxGroupHobbies.items.Add(cBoxSports);
            cBoxGroupHobbies.columns = 1;

            radioMan.boxLabel = "Man";
            radioMan.dataMember = "Man";
            radioMan.name = "GenderRadio";
            radioWoman.boxLabel = "Woman";
            radioWoman.dataMember = "Woman";
            radioWoman.name = "GenderRadio";
            rgGender.fieldLabel = "Gender";
            rgGender.dataMember = "Gender";
            rgGender.items.Add(radioMan);
            rgGender.items.Add(radioWoman);

            txtNotes.fieldLabel = "Notes";
            txtNotes.dataMember = "Notes";
            txtNotes.height = 60;

            tfReference.fieldLabel = "Reference";
            tfReference.dataMember = "Reference";
            tfReference.displayField = "Name";
            tfReference.readOnly = true;
            tfReference.triggerClass = "x-form-search-trigger";
            tfReference.listeners.onTriggerFired = new ExternalJs("findReference");

            this.defaults = BodyStyle.DEFAULT_PADDING;
            //this.formPanel.items.Add(txtName);
            //this.formPanel.items.Add(txtLastName);
            //this.formPanel.items.Add(dtBirthday);
            //this.formPanel.items.Add(rgGender);
            //this.formPanel.items.Add(tfReference);
            //this.formPanel.items.Add(nfYearsOfExprience);
            //this.formPanel.items.Add(mfSalary);
            //this.formPanel.items.Add(cmbPhoneNumer);
            //this.formPanel.items.Add(cBoxMember);
            //this.formPanel.items.Add(cBoxGroupHobbies);
            //this.formPanel.items.Add(txtNotes);

            this.pnlInner.layout = "form";
            this.pnlInner.items.Add(txtName);
            this.pnlInner.items.Add(txtLastName);
            this.pnlInner.items.Add(dtBirthday);
            this.pnlInner.items.Add(rgGender);
            this.pnlInner.items.Add(tfReference);
            this.pnlInner.items.Add(nfYearsOfExprience);
            this.pnlInner.items.Add(mfSalary);
            this.pnlInner.items.Add(cmbPhoneNumer);
            this.pnlInner.items.Add(cBoxMember);
            this.pnlInner.items.Add(cBoxGroupHobbies);
            this.pnlInner.items.Add(txtNotes);
            this.formPanel.items.Add(pnlInner);
        }

        private void BuildPhoneNumberCombo()
        {
            Record phoneNumberRecord = new Record();
            phoneNumberRecord.fields.Add(new FieldDefinition() { name = "Id", type = FieldDefinitionType.intt, sortDir = SortDirection.ASC });
            phoneNumberRecord.fields.Add(new FieldDefinition() { name = "Number", type = FieldDefinitionType.stringg, sortDir = SortDirection.ASC });

            JsonReader phoneNumberReader = new JsonReader();
            phoneNumberReader.recordType = phoneNumberRecord;

            var phoneNumbers = new[] { new { Id = 1, Number = "555-123456789" }, 
                                       new { Id = 2, Number = "444-123456789" } 
                                     };

            phoneNumerStore.reader = phoneNumberReader;
            phoneNumerStore.data = phoneNumbers;

            cmbPhoneNumer.fieldLabel = "Phone Number";
            cmbPhoneNumer.dataMember = "PhoneNumber";
            cmbPhoneNumer.displayField = "Number";
            cmbPhoneNumer.valueField = "Id";
            cmbPhoneNumer.store = phoneNumerStore;
            cmbPhoneNumer.mode = "local";
            cmbPhoneNumer.triggerAction = "all";
            cmbPhoneNumer.forceSelection = true;
            cmbPhoneNumer.minChars = 4;
        }
    }
}